// pages/manage/withdrawal/lookup.js
var util = require('../../../utils/util')
var app = getApp()
var suffix;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: '',
    img: [],
    voucher: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let data = JSON.parse(options.data)
    suffix=''
    if(data.suffix){
      suffix=data.suffix
    }
    util.request('/sales/detail', {
      openid: data.openid,
      suffix: suffix
    }, 'GET').then(res => {
      console.log(res.data)
      data = Object.assign(data, res.data[0])
      let img = [];
      img.push(data.payment_img)
      var voucher = ''
      if (data.voucher) voucher = util.link(data.voucher)
      that.setData({
        data: data,
        img: img,
        voucher: voucher
      })
    })
  },
  previewImg(e) {
    var img = this.data.img;
    for (let i in img) {
      img[i] = util.link(img[i])
      this.setData({
        img: img
      })
    }
    wx.previewImage({
      urls: img,
      current: img[e.currentTarget.dataset.index]
    });
  },
  preview(e) {
    console.log(e)
    let arr = []
    arr.push(this.data.voucher)
    wx.previewImage({
      urls: arr,
      current: arr[0]
    });
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.data.phone,
    })
  },
  confirm: function () {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo')
    var data = that.data.data;
    wx.showModal({
      title: '已付款给用户',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '确认中',
          })
          util.request('/sales/update/income', {
            openid: that.data.data.openid,
            withdrawal: that.data.data.fee,
            suffix: suffix,
            type: 'withdrawal'
          }, 'POST').then(res => {
            util.request('/withdrawal/update', {
              id: that.data.data.w_id,
              status: 'success',
              suffix: suffix,
              operate_name: userInfo.nickName,
              operate_openid: app.globalData.openid
            }, 'POST').then(res => {
              let nowDate = util.formatTime(new Date())
              that.sendMessage(data.openid, data.fee, data.name, data.creation_date, nowDate)
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showToast({
                title: '确认成功',
                icon: 'success',
                duration: 2000
              })
              data.status = 'success'
              data.operate_name = userInfo.nickName
              data.update_date = nowDate
              that.setData({
                data: data
              })
            })
          })
        }
      }
    })
  },
  sendMessage: function (openid, fee, name, c_date, o_date) {
    wx.cloud.callFunction({
      name: 'sendMessage',
      data: {
        openid: openid,
        page: 'pages/salesperson/salesperson',
        data: {
          "amount1": {
            "value": fee
          },
          "thing9": {
            "value": name
          },
          "time7": {
            "value": c_date
          },
          "time8": {
            "value": o_date
          },
        },
        templateId: '5J1Vm_GHvcTOS57iC52BM12ave8-mI1Mx9zl6iDghpI'
      }
    }).then(res => {})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  addImage: function () {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        var tempFilePaths = res.tempFilePaths
        console.log(res.tempFilePaths)
        that.setData({
          voucher: tempFilePaths[0]
        })
        wx.uploadFile({
          url: app.globalData.server + '/upload/image', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'catalog': 'voucher'
          },
          success(res) {
            console.log(res)
            util.request('/withdrawal/custom', {
              query: 'update warranty_withdrawal set voucher="' + res.data + '" where id=' + that.data.data.w_id + ' '
            }, 'POST').then(res => {
              console.log(res)
            })
          },
          fail: function (res) {
            console.log(res)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})